{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "65495e35",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c']\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a']\n",
      "['o', 'n', 't', ' ', 'p', 'a']\n",
      "['o', 'n', ' ', 't', 'a', 'p']\n",
      "on tap\n",
      "------\n",
      "Don't panic\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c']\n",
      "on\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c']\n",
      "on tap\n"
     ]
    }
   ],
   "source": [
    "# 目标：“Don't panic\" ==> \"on tap\"\n",
    "\n",
    "# 方法一： 使用list的方法完成\n",
    "phrase = \"Don't panic\"\n",
    "plist = list(phrase) # list（） 可以将 str to list\n",
    "print(phrase)\n",
    "print(plist)\n",
    "\n",
    "# 1.删除后面三个元素 n i c\n",
    "for i in range(3):\n",
    "    plist.pop()\n",
    "print(plist)\n",
    "# 2. 删除D ‘\n",
    "plist.remove('D')\n",
    "plist.pop(2)\n",
    "print(plist)\n",
    "# 3. 数学案例： 计算 （（a+b) *(c+d))\n",
    "plist.extend([plist.pop(), plist.pop()])\n",
    "#                   a    ,       p\n",
    "# 4. 数据插入  实现：’t‘ 和’‘  的位置转换\n",
    "# plist.insert(2, plist.pop(3))\n",
    "plist.insert(3, plist.pop(2))\n",
    "# 5. list to str\n",
    "new_plist = ''.join(plist)\n",
    "print(plist)\n",
    "print(new_plist)\n",
    "\n",
    "# plist 改变了原先的状态么？ 列表的方法会改变列表本身，如果不想打破或者想保留原先的数据内容，请先使用.copy\n",
    "\n",
    "print(\"------\")\n",
    "# 方法二： 使用切片的方法实验\n",
    "phrase = \"Don't panic\"\n",
    "plist = list(phrase) # list（） 可以将 str to list\n",
    "print(phrase)\n",
    "print(plist)\n",
    "\n",
    "on_str = ''.join(plist[1:3])\n",
    "print(on_str)\n",
    "new_phrase = on_str + ''.join([plist[5] + plist[4] +plist[7] +plist[6]])\n",
    "print(plist)\n",
    "print(new_phrase)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2d013c2b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
